package com.campus.client.view;

import com.campus.client.controller.AdminMainController;
import com.campus.client.controller.TeacherMainController;
import com.campus.client.net.ClientConnector;
import com.campus.common.vo.User;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

public class AdminMainFrame {

    Button changePasswordBtn = new Button("修改密码");
    Button logoutBtn = new Button("退出登录");
    Button accountManagementBtn = new Button("账号管理");
    Button courseSystemBtn = new Button("课程系统");
    Button libraryBtn = new Button("图书馆系统");
    Button storeBtn = new Button("商店系统");

    private User user = new User();

    public User getUser() {
        return user;
    }

    public void setUser(String Id, String password, String role) {
        this.user.setId(Id);
        this.user.setPassword(password);
        this.user.setRole(role);
        System.out.print("用户名" + this.user.getId());
    }

    private AdminMainController controller;

    public void show(Stage primaryStage) throws IOException {
        // 创建主VBox容器
        VBox mainVBox = new VBox();

        ClientConnector connector = new ClientConnector("172.20.10.2", 8888);
        controller = new AdminMainController(this, connector);

        mainVBox.setPrefHeight(500.0);
        mainVBox.setPrefWidth(802.0);

        // 创建AnchorPane
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMaxHeight(Double.NEGATIVE_INFINITY);
        anchorPane.setMaxWidth(Double.NEGATIVE_INFINITY);
        anchorPane.setPrefHeight(500.0);
        anchorPane.setPrefWidth(802.0);
        AnchorPane.setTopAnchor(anchorPane, 0.0);
        AnchorPane.setBottomAnchor(anchorPane, 0.0);
        AnchorPane.setLeftAnchor(anchorPane, 0.0);
        AnchorPane.setRightAnchor(anchorPane, 0.0);

        // 创建背景图片
        ImageView backgroundImage = new ImageView();
        try {
            Image bgImage = new Image("background.png");
            backgroundImage.setImage(bgImage);
        } catch (Exception e) {
            System.out.println("无法加载背景图片: " + e.getMessage());
        }
        backgroundImage.setFitHeight(500.0);
        backgroundImage.setFitWidth(802.0);
        backgroundImage.setLayoutY(55.0);
        backgroundImage.setPreserveRatio(true);
        backgroundImage.setPickOnBounds(true);

        // 创建左侧半透明矩形
        Rectangle leftRect = new Rectangle();
        leftRect.setArcHeight(5.0);
        leftRect.setArcWidth(5.0);
        leftRect.setFill(Color.web("#d5e6f6"));
        leftRect.setHeight(432.0);
        leftRect.setLayoutY(68.0);
        leftRect.setOpacity(0.51);
        leftRect.setStroke(Color.BLACK);
        leftRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        leftRect.setWidth(233.0);

        // 创建顶部蓝色矩形
        Rectangle topRect = new Rectangle();
        topRect.setArcHeight(5.0);
        topRect.setArcWidth(5.0);
        topRect.setFill(Color.web("#044684"));
        topRect.setHeight(68.0);
        topRect.setStroke(Color.BLACK);
        topRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        topRect.setWidth(802.0);

        // 创建标题文本
        Text titleText = new Text("虚拟校园系统");
        titleText.setFill(Color.WHITE);
        titleText.setLayoutX(321.0);
        titleText.setLayoutY(46.0);
        titleText.setStrokeWidth(0.0);
        titleText.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        titleText.setFont(Font.font("System", FontWeight.BOLD, 30.0));

        // 创建修改密码按钮
        changePasswordBtn.setLayoutX(20.0);
        changePasswordBtn.setLayoutY(460.0);
        changePasswordBtn.setMnemonicParsing(false);

        // 创建退出登录按钮
        logoutBtn.setLayoutX(155.0);
        logoutBtn.setLayoutY(460.0);
        logoutBtn.setMnemonicParsing(false);

        // 创建账号管理HBox
        HBox accountManagementHBox = new HBox();
        accountManagementHBox.setAlignment(Pos.CENTER);
        accountManagementHBox.setLayoutY(87.0);
        accountManagementHBox.setPrefHeight(48.0);
        accountManagementHBox.setPrefWidth(233.0);
        accountManagementHBox.setSpacing(10.0);

        // 账号管理图标
        ImageView accountManagementIcon = new ImageView();
        try {
            Image accountManagementImage = new Image("account.png");
            accountManagementIcon.setImage(accountManagementImage);
        } catch (Exception e) {
            System.out.println("无法加载账号管理图标: " + e.getMessage());
        }
        accountManagementIcon.setFitHeight(58.0);
        accountManagementIcon.setFitWidth(66.0);
        accountManagementIcon.setPreserveRatio(true);
        accountManagementIcon.setPickOnBounds(true);

        // 账号管理按钮
        accountManagementBtn.setAlignment(Pos.CENTER);
        accountManagementBtn.setMnemonicParsing(false);
        accountManagementBtn.setPrefHeight(42.0);
        accountManagementBtn.setPrefWidth(135.0);
        accountManagementBtn.setStyle("-fx-background-color: #1E90FF;");
        accountManagementBtn.setTextFill(Color.WHITE);
        accountManagementBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        accountManagementBtn.setOnAction(e -> {
            try {
                showAccountManagementFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });


        accountManagementHBox.getChildren().addAll(accountManagementIcon, accountManagementBtn);

        // 创建课程系统HBox
        HBox courseSystemHBox = new HBox();
        courseSystemHBox.setAlignment(Pos.CENTER);
        courseSystemHBox.setLayoutX(1.0);
        courseSystemHBox.setLayoutY(152.0);
        courseSystemHBox.setPrefHeight(42.0);
        courseSystemHBox.setPrefWidth(231.0);
        courseSystemHBox.setSpacing(10.0);

        // 课程系统图标
        ImageView courseSystemIcon = new ImageView();
        try {
            Image courseSystemImage = new Image("teaching.png");
            courseSystemIcon.setImage(courseSystemImage);
        } catch (Exception e) {
            System.out.println("无法加载课程系统图标: " + e.getMessage());
        }
        courseSystemIcon.setFitHeight(58.0);
        courseSystemIcon.setFitWidth(66.0);
        courseSystemIcon.setPreserveRatio(true);
        courseSystemIcon.setPickOnBounds(true);

        // 课程系统按钮
        courseSystemBtn.setAlignment(Pos.CENTER);
        courseSystemBtn.setMnemonicParsing(false);
        courseSystemBtn.setPrefHeight(42.0);
        courseSystemBtn.setPrefWidth(135.0);
        courseSystemBtn.setStyle("-fx-background-color: #1E90FF;");
        courseSystemBtn.setTextFill(Color.WHITE);
        courseSystemBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        courseSystemBtn.setOnAction(e -> {
            try {
                showAdminCourseFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        courseSystemHBox.getChildren().addAll(courseSystemIcon, courseSystemBtn);

        // 创建图书馆系统HBox
        HBox libraryHBox = new HBox();
        libraryHBox.setAlignment(Pos.CENTER);
        libraryHBox.setLayoutX(1.0);
        libraryHBox.setLayoutY(218.0);
        libraryHBox.setPrefHeight(36.0);
        libraryHBox.setPrefWidth(231.0);
        libraryHBox.setSpacing(10.0);

        // 图书馆系统图标
        ImageView libraryIcon = new ImageView();
        try {
            Image libraryImage = new Image("library.png");
            libraryIcon.setImage(libraryImage);
        } catch (Exception e) {
            System.out.println("无法加载图书馆系统图标: " + e.getMessage());
        }
        libraryIcon.setFitHeight(58.0);
        libraryIcon.setFitWidth(66.0);
        libraryIcon.setPreserveRatio(true);
        libraryIcon.setPickOnBounds(true);

        // 图书馆系统按钮
        libraryBtn.setAlignment(Pos.CENTER);
        libraryBtn.setMnemonicParsing(false);
        libraryBtn.setPrefHeight(42.0);
        libraryBtn.setPrefWidth(135.0);
        libraryBtn.setStyle("-fx-background-color: #1E90FF;");
        libraryBtn.setTextFill(Color.WHITE);
        libraryBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        libraryBtn.setOnAction(e -> {
            try {
                showLibraryFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        libraryHBox.getChildren().addAll(libraryIcon, libraryBtn);

        // 创建商店系统HBox
        HBox storeHBox = new HBox();
        storeHBox.setAlignment(Pos.CENTER);
        storeHBox.setLayoutX(1.0);
        storeHBox.setLayoutY(281.0);
        storeHBox.setPrefHeight(48.0);
        storeHBox.setPrefWidth(231.0);
        storeHBox.setSpacing(10.0);

        // 商店系统图标
        ImageView storeIcon = new ImageView();
        try {
            Image storeImage = new Image("shop.png");
            storeIcon.setImage(storeImage);
        } catch (Exception e) {
            System.out.println("无法加载商店系统图标: " + e.getMessage());
        }
        storeIcon.setFitHeight(58.0);
        storeIcon.setFitWidth(66.0);
        storeIcon.setPreserveRatio(true);
        storeIcon.setPickOnBounds(true);

        // 商店系统按钮
        storeBtn.setAlignment(Pos.CENTER);
        storeBtn.setMnemonicParsing(false);
        storeBtn.setPrefHeight(42.0);
        storeBtn.setPrefWidth(135.0);
        storeBtn.setStyle("-fx-background-color: #1E90FF;");
        storeBtn.setTextFill(Color.WHITE);
        storeBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        storeBtn.setOnAction(e -> {
            try {
                showStoreFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        storeHBox.getChildren().addAll(storeIcon, storeBtn);

        // 创建校徽图标
        ImageView schoolBadgeIcon = new ImageView();
        try {
            Image schoolBadgeImage = new Image("badge.png");
            schoolBadgeIcon.setImage(schoolBadgeImage);
        } catch (Exception e) {
            System.out.println("无法加载校徽图标: " + e.getMessage());
        }
        schoolBadgeIcon.setFitHeight(58.0);
        schoolBadgeIcon.setFitWidth(51.0);
        schoolBadgeIcon.setLayoutX(14.0);
        schoolBadgeIcon.setLayoutY(9.0);
        schoolBadgeIcon.setPreserveRatio(true);
        schoolBadgeIcon.setPickOnBounds(true);

        // 将所有组件添加到AnchorPane
        anchorPane.getChildren().addAll(
                backgroundImage, leftRect, topRect, titleText,
                changePasswordBtn, logoutBtn, accountManagementHBox,
                courseSystemHBox, libraryHBox, storeHBox, schoolBadgeIcon
        );

        // 将AnchorPane添加到主VBox
        mainVBox.getChildren().add(anchorPane);

        // 创建场景并显示
        Scene scene = new Scene(mainVBox);
        primaryStage.setTitle("虚拟校园系统 - 管理员版");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void setChangePasswordButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        changePasswordBtn.setOnAction(handler::accept);
    }

    public void setLogoutButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        logoutBtn.setOnAction(handler::accept);
    }

    public void showLoginFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) logoutBtn.getScene().getWindow();

        // 创建主界面
        LoginFrame loginFrame = new LoginFrame();
        Stage loginStage = new Stage();

        // 显示主界面
        loginFrame.start(loginStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showPasswordChangeFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) logoutBtn.getScene().getWindow();

        // 创建主界面
        PasswordChangeFrame passwordChangeFrame = new PasswordChangeFrame();
        passwordChangeFrame.setPassword(user.getPassword());
        passwordChangeFrame.setUsername(user.getId());
        passwordChangeFrame.setRole("管理员");
        Stage passwordChangeStage = new Stage();

        // 显示主界面
        passwordChangeFrame.start(passwordChangeStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showAdminCourseFrame() throws IOException {
        Stage registerStage = (Stage) courseSystemBtn.getScene().getWindow();
        AdminCourseFrame adminCourseFrame = new AdminCourseFrame();
        adminCourseFrame.setUser(user.getId(), user.getPassword(), user.getRole());
        Stage adminCourseStage = new Stage();

        adminCourseFrame.start(adminCourseStage);
        registerStage.close();
    }

    public void showStoreFrame() throws IOException {
        Stage registerStage = (Stage) storeBtn.getScene().getWindow();
        ShopAdminFrame shopAdminFrame = new ShopAdminFrame();
        shopAdminFrame.setUser(user.getId(), user.getPassword(), user.getRole());
        Stage shopAdminStage = new Stage();

        shopAdminFrame.start(shopAdminStage);
        registerStage.close();
    }

    public void showLibraryFrame() throws Exception {
        Stage registerStage = (Stage) libraryBtn.getScene().getWindow();
        LibraryAdminFrame libraryAdminFrame = new LibraryAdminFrame();
        libraryAdminFrame.setUser(user.getId(), user.getPassword(), user.getRole());
        Stage libraryAdminStage = new Stage();

        libraryAdminFrame.start(libraryAdminStage);
        registerStage.close();
    }

    public void showAccountManagementFrame() throws Exception {
        Stage registerStage = (Stage) accountManagementBtn.getScene().getWindow();
        AccountManagementFrame accountManagementFrame = new AccountManagementFrame();
        accountManagementFrame.setUser(user.getId(), user.getPassword(), user.getRole());
        Stage accountManagementStage = new Stage();

        accountManagementFrame.start(accountManagementStage);
        registerStage.close();
    }
}